﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;
using ProofreaderObj = AlexandriaPressAdminObject.ProofReaderInfo;
using MailSender;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm14 : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType != "Type_Admin")
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

                tableShow.Visible = false;
                
            }
            
            gridAccountInfo.Visible = false;
            lblCheck.Visible = false;
            lblLoginCheck.Visible = false;
            
        }

        private void BindData()
        {
            gridProofReaderList.DataSource = ProofReaderBL.viewProofReaderList();
            gridProofReaderList.EmptyDataText = "No members in proofreader list";
            gridProofReaderList.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            tableShow.Visible = true;

        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            tableShow.Visible = false;
            int countCheck = 0;
            List<ProofreaderObj> proofreader = new List<ProofreaderObj>();
            ProofreaderObj proofObj;
            for (int i = 0; i < gridProofReaderList.Rows.Count; i++)
            {
                CheckBox selected = (CheckBox)gridProofReaderList.Rows[i].FindControl("cbSelect");
                if (selected.Checked)
                {
                    proofObj = new ProofreaderObj();
                    proofObj.ID = Convert.ToInt32(gridProofReaderList.DataKeys[i].Value);
                    proofreader.Add(proofObj);
                    countCheck++;
                }
            }

            if (countCheck > 0)
            {
                int count = ProofReaderBL.RemoveProofreader(proofreader);
                lblCheck.Text = count + " proofreader/s removed";
                lblCheck.Visible = true;
                BindData();
            }
            else
            {
            lblCheck.Text="No data selected";
            lblCheck.Visible = true;
            }
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            if (ProofReaderBL.CheckEmail(TextBox3.Text) <= 0)
            {
                List<ProofreaderObj> proofList = new List<ProofreaderObj>();
                ProofreaderObj accountInfo = new ProofreaderObj();
                accountInfo.FirstName = TextBox1.Text;
                accountInfo.LastName = TextBox2.Text;
                accountInfo.Name = TextBox1.Text + " " + TextBox2.Text;
                accountInfo.Gender = DropDownList1.SelectedValue;
                accountInfo.Birthday = Convert.ToDateTime(DropDownList2.SelectedValue + "/" + DropDownList3.SelectedValue + "/" + TextBox5.Text);
                accountInfo.Email = TextBox3.Text;
                accountInfo.Password = CreateRandomPassword();
                proofList.Add(accountInfo);

                UserBusinsessLayer business = new UserBusinsessLayer();
                business.createUser(accountInfo.FirstName, accountInfo.LastName, accountInfo.Gender, accountInfo.Email, accountInfo.Birthday, DateTime.Now, "n/a", accountInfo.Email, accountInfo.Password, "n/a", "n/a");

                int check = ProofReaderBL.UpdateProof(accountInfo.Email);
                if (check > 0)
                {
                    gridAccountInfo.DataSource = proofList;
                    gridAccountInfo.DataBind();
                    EraseInput();
                    gridAccountInfo.Visible = true;
                    tableShow.Visible = false;
                    BindData();
                    lblCheck.Visible = true;
                    lblCheck.Text = "Login created";
                    EmailNotification.AdminMail("admin@alexandria.com", accountInfo.Email, null, null, "ProofReader Login", "This is a copy of your default login. You may update this in your profile" + "</br>" + "Username: " + accountInfo.Email + "</br>" + "Password: " + accountInfo.Password);

                }
                else
                {
                    lblCheck.Visible = true;
                    lblCheck.Text = "Login creation failed";
                }
            }
            else
            {
                lblLoginCheck.Visible = true;
                lblLoginCheck.Text = "Email is already in use";
            }
            BindData();
        }

        private static string CreateRandomPassword()
        {
            string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            char[] chars = new char[8];
            Random rd = new Random();
            for (int i = 0; i < 8; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }
            return new string(chars);
        }

        private void EraseInput()
        {
            TextBox1.Text = null;
            TextBox2.Text = null;
            TextBox3.Text = null;
            TextBox4.Text = null;
            TextBox5.Text = null;
            DropDownList1.SelectedIndex = 0;
            DropDownList2.SelectedIndex = 0;
            DropDownList3.SelectedIndex = 0;

        }

        protected void gridProofReaderList_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridAccountInfo.Visible = true;
            gridAccountInfo.PageIndex = e.NewPageIndex;
            gridAccountInfo.DataSource = ProofReaderBL.viewProofReaderList();
            gridAccountInfo.DataBind();
        
        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {
            if (ProofReaderBL.CheckEmail(TextBox3.Text) > 0)
            {
                lblLoginCheck.Visible = true;
                lblLoginCheck.Text = "Email is already in use";
            }
        }
    }
}
